package com.ruoyi.system.service.impl;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.ruoyi.system.domain.EpmInformation;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.EpmInformationMapper;
import com.ruoyi.system.mapper.SysPostMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EpmInformationHistoryMapper;
import com.ruoyi.system.domain.EpmInformationHistory;
import com.ruoyi.system.service.IEpmInformationHistoryService;
import com.ruoyi.common.core.controller.BaseController;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 员工历史信息Service业务层处理
 * 
 * @author ruoyi
 * &#064;date  2024-05-21
 */
@Service
public class EpmInformationHistoryServiceImpl implements IEpmInformationHistoryService 
{
    @Autowired
    private EpmInformationHistoryMapper epmInformationHistoryMapper;

    @Autowired
    private EpmInformationMapper epmInformationMapper;
    @Autowired
    private SysPostMapper postMapper;
    /**
     * 查询单个员工历史信息
     * 
     * @param userId 员工历史信息主键
     * @return 员工历史信息
     */
    @Override
    public List<EpmInformationHistory> selectEpmInformationHistoryById(int userId)
    {
        return epmInformationHistoryMapper.selectEpmInformationHistoryById(userId);
    }

    /**
     * 查询员工历史信息的员工列表
     *
     * @param epmInformationHistory 员工历史信息
     * @return 员工历史信息
     */
    @Override
    public List<EpmInformation> selectEpmInformationHistoryList(EpmInformationHistory epmInformationHistory)
    {

        return epmInformationHistoryMapper.selectEpmInformationHistoryList(epmInformationHistory);
    }


      /**
          * 新增员工历史信息
          *
          * @param epmInformationHistory 员工历史信息
          * @return 结果
          */
    @Override
    public int insertEpmInformationHistory(EpmInformationHistory epmInformationHistory,int operation_status)
   {
    //操作状态operation_status（入职1，离职2，基本信息修改3，部门修改4，岗位修改5）
     epmInformationHistory.setOperationTime(Timestamp.from(Instant.now()));
       epmInformationHistory.setOperationStatus(operation_status);
       if(operation_status==1){

           return epmInformationHistoryMapper.insertEpmInformationHistory(epmInformationHistory);
       }
       if(operation_status==2){

           return epmInformationHistoryMapper.insertEpmInformationHistory(epmInformationHistory);
       }
       if(operation_status==3){

           return epmInformationHistoryMapper.insertEpmInformationHistory(epmInformationHistory);
       }
       if(operation_status==4){

           return epmInformationHistoryMapper.insertEpmInformationHistory(epmInformationHistory);
       }
       if(operation_status==5){

           return epmInformationHistoryMapper.insertEpmInformationHistory(epmInformationHistory);
       }

        return epmInformationHistoryMapper.insertEpmInformationHistory(epmInformationHistory);
    }

    /**
     * 通过输入epmInformation检查修改情况
     *
     * @param epmInformation 员工信息

     */
    @Override
    public void check_change(EpmInformation epmInformation){
        EpmInformation epmInformation_ord= epmInformationMapper.selectEpmInformationById(epmInformation.getUserId());
        check_Information_change(epmInformation,epmInformation_ord);
        check_dept_change(epmInformation,epmInformation_ord);
        check_post_change(epmInformation,epmInformation_ord);

    }

    /**
     * 检验基本信息是否修改，如果修改了，就进行记录，
     *
     * @param epmInformation,epmInformation_ord 员工信息

     */
        @Override
        public void check_Information_change(EpmInformation epmInformation, EpmInformation epmInformation_ord) {
            if(!check_Information_equal(epmInformation,epmInformation_ord)){
                //将修改员工信息记录日志
                EpmInformationHistory epmInformationHistory=new EpmInformationHistory();
                //将epmInformation复制到epmInformationHistory
                BeanUtils.copyProperties(epmInformation,epmInformationHistory);
                insertEpmInformationHistory(epmInformationHistory,3);
            }

        }


    /**
     * 检验岗位是否修改，如果修改了，就进行记录，
     *
     * @param epmInformation 员工信息
     */
    @Override
    public void check_post_change(EpmInformation epmInformation, EpmInformation epmInformation_ord)
    {
            //查询旧的员工信息
            //EpmInformation epmInformation_ord= epmInformationMapper.selectEpmInformationById(epmInformation.getUserId());
            //查询岗位信息并赋值
            List<Long> list=postMapper.selectPostListByUserId(epmInformation_ord.getUserId());
            if (list != null && !list.isEmpty()) {
            Long[] array = list.toArray(new Long[0]);
            epmInformation_ord.setPostIds(array);
                  } else {
            epmInformation_ord.setPostIds(null);
                  }
           //判断是否有变化
            if(!check_post_equal(epmInformation.getPostIds(), epmInformation_ord.getPostIds())){


                //将修改员工信息记录日志
                EpmInformationHistory epmInformationHistory=new EpmInformationHistory();
                //将epmInformation复制到epmInformationHistory
                BeanUtils.copyProperties(epmInformation,epmInformationHistory);

                epmInformationHistory.setPostName(Generate_post_record(epmInformation_ord.getPostIds(),epmInformation.getPostIds()));

                insertEpmInformationHistory(epmInformationHistory,5);
            }


    }

    /**
     * 检验部门是否修改，如果修改了，就进行记录，
     *
     * @param epmInformation 员工信息
     */
    @Override
    public void check_dept_change(EpmInformation epmInformation, EpmInformation epmInformation_ord)
    {

            //EpmInformation epmInformation_ord= epmInformationMapper.selectEpmInformationById(epmInformation.getUserId());
            //System.out.println(epmInformation_ord);
            if(!Objects.equals(epmInformation.getDeptId(), epmInformation_ord.getDeptId())){

                //将修改员工信息记录日志
                EpmInformationHistory epmInformationHistory=new EpmInformationHistory();
                //将epmInformation复制到epmInformationHistory
                epmInformationHistory.setDeptName(epmInformation.getDeptName());
                BeanUtils.copyProperties(epmInformation,epmInformationHistory);

                insertEpmInformationHistory(epmInformationHistory,4);

        }
    }

    /**
     * 生成岗位记录信息
     *
     * @param a,b  历史信息，更新的信息
     * @return 生成的字符串
     */
    @Override
    public  String  Generate_post_record(Long[] a, Long[] b) {

        List<SysPost> posts = postMapper.selectPostAll();

        Map<Long, String> postMap = posts.stream().collect(Collectors.toMap(SysPost::getPostId, SysPost::getPostName));
        StringBuilder output = new StringBuilder();
        output.append("员工岗位由 ");
        if (a == null ) {
            output.append("无 ");
        }else {
            for (Long postId : a) {
                String postName = postMap.get(postId);
                output.append(postName).append("，");
            }
        }

        output.append("调整为 ");
        if (b == null) {
            output.append("无 ");
        }else{
            for (Long postId : b) {
                String postName = postMap.get(postId);
                output.append(postName).append("，");
            }
        }

        return output.toString();
    }


    /**
     * 比较历史岗位与现岗位
     *
     * @param a,b 员工信息
     * @return 岗位是否发生变化true/false
     */
    @Override
    public  boolean  check_post_equal(Long[] a, Long[] b) {
        if (a == null && b == null) {
            return true;

        } else if((a == null) ^ (b == null)) {
            return false;
        } else {
            List<Long> list1 = new ArrayList<>(Arrays.asList(a));
            List<Long> list2 = new ArrayList<>(Arrays.asList(b));
            return list1.containsAll(list2) && list2.containsAll(list1);
        }
    }

    /**
     * 检查两个EPMInformation的实体是否相等
     *
     * @param EpmInformation1,EpmInformation2 员工信息
     * @return 相等时返回true/否则false
     */
    @Override
    public  boolean  check_Information_equal(EpmInformation EpmInformation1,EpmInformation EpmInformation2){
        if (EpmInformation1 == null && EpmInformation2 == null) {
            return true;
        }
        if ((EpmInformation1 == null) ^ (EpmInformation2 == null)) {
            return false;
        }
        if (EpmInformation1 == EpmInformation2) {
            return true;
        }
        if (!Objects.equals(EpmInformation1.getId(), EpmInformation2.getId())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getUserId(), EpmInformation2.getUserId())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getName(), EpmInformation2.getName())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getDeptId(), EpmInformation2.getDeptId())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getEmpNumber(), EpmInformation2.getEmpNumber())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getSex(), EpmInformation2.getSex())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getIdCard(), EpmInformation2.getIdCard())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getIdNumberRegion(), EpmInformation2.getIdNumberRegion())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getPhoneNumber(), EpmInformation2.getPhoneNumber())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getRegion(), EpmInformation2.getRegion())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getContractDate(), EpmInformation2.getContractDate())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getContractLong(), EpmInformation2.getContractLong())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getDate(), EpmInformation2.getDate())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getTypeOfEmployment(), EpmInformation2.getTypeOfEmployment())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getTypeOfMen(), EpmInformation2.getTypeOfMen())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getMaritalStatus(), EpmInformation2.getMaritalStatus())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getPoliticalLandscape(), EpmInformation2.getPoliticalLandscape())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getAddress(), EpmInformation2.getAddress())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getHighEducationalLevel(), EpmInformation2.getHighEducationalLevel())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getSchool(), EpmInformation2.getSchool())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getMajor(), EpmInformation2.getMajor())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getNation(), EpmInformation2.getNation())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getEmergencyContact(), EpmInformation2.getEmergencyContact())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getEmergencyContactPhone(), EpmInformation2.getEmergencyContactPhone())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getFirstDegree(), EpmInformation2.getFirstDegree())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getSecondDegree(), EpmInformation2.getSecondDegree())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getTypeEducation(), EpmInformation2.getTypeEducation())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getBank(), EpmInformation2.getBank())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getBankAccountNumber(), EpmInformation2.getBankAccountNumber())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getHobby(), EpmInformation2.getHobby())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getDormitoryNeed(), EpmInformation2.getDormitoryNeed())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getDormitory(), EpmInformation2.getDormitory())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getBirthday(), EpmInformation2.getBirthday())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getOther(), EpmInformation2.getOther())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved1(), EpmInformation2.getReserved1())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved2(), EpmInformation2.getReserved2())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved3(), EpmInformation2.getReserved3())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved4(), EpmInformation2.getReserved4())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved5(), EpmInformation2.getReserved5())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved6(), EpmInformation2.getReserved6())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved7(), EpmInformation2.getReserved7())) {
            return false;
        }
        if (!Objects.equals(EpmInformation1.getReserved8(), EpmInformation2.getReserved8())) {
            return false;
        }
        return true;
    }

    @Override
    public void insert_epm_out(List<Integer> userIds,String Username) {
        if (!userIds.isEmpty()) {
            for (Integer userId : userIds) {

                EpmInformationHistory epmInformationHistory=new EpmInformationHistory();
                epmInformationHistory.setUpdateBy(Username);
                epmInformationHistory.setUserId(Long.valueOf(userId));
                insertEpmInformationHistory(epmInformationHistory,2);

            }
        }

    }
}
